/*******************************************************************************
* 
* A simple string utility class
* 
* 
******************************************************************************/
#include "strings.h"

#include <windows.h>

namespace noria{ namespace text
{
    string Strings::w2a(const wstring& text)
    {
        int nbytes = WideCharToMultiByte( CP_UTF8, // specify the code page used to perform the conversion

            0,         // no special flags to handle unmapped characters

            text.c_str(),     // wide character string to convert

            text.length(),   // the number of wide characters in that string

            NULL,      // no output buffer given, we just want to know how long it needs to be

            0,

            NULL,      // no replacement character given

            NULL );    // we don't want to know if a character didn't make it through the translation

        // make sure the buffer is big enough for this, making it larger if necessary
        if(!nbytes)
            return string();


        string buffer(nbytes, 0);

        WideCharToMultiByte( CP_UTF8, // specify the code page used to perform the conversion

            0,         // no special flags to handle unmapped characters

            text.c_str(),   // wide character string to convert

            text.length(),   // the number of wide characters in that string

            &buffer[0], // put the output ascii characters at the end of the buffer

            nbytes,                           // there is at least this much space there

            NULL,      // no replacement character given

            NULL );

        return buffer;
    }

    wstring Strings::a2w(const string& text)
    {
        int nbytes = MultiByteToWideChar( CP_UTF8, // specify the code page used to perform the conversion

            0,         // no special flags to handle unmapped characters

            text.c_str(),     // wide character string to convert

            text.length(),   // the number of wide characters in that string

            NULL,      // no output buffer given, we just want to know how long it needs to be

            0);    // we don't want to know if a character didn't make it through the translation

        // make sure the buffer is big enough for this, making it larger if necessary
        if(!nbytes)
            return wstring();


        wstring buffer(nbytes, 0);

        MultiByteToWideChar( CP_UTF8, // specify the code page used to perform the conversion

            0,         // no special flags to handle unmapped characters

            text.c_str(),   // wide character string to convert

            text.length(),   // the number of wide characters in that string

            &buffer[0], // put the output ascii characters at the end of the buffer

            nbytes);

        return buffer;
    }
}}